﻿
using Steeltoe.Common.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shop.Help
{
    public static class Map
    {
        private const double EARTH_RADIUS = 6378.137; //地球半径
        
        private static double rad(double d)
        {
            return d * Math.PI / 180.0;
        }

        /// <summary>
        ///  计算两个点之间的距离
        /// </summary>
        /// <param name="lat1"></param>
        /// <param name="lon1"></param>
        /// <param name="lat2"></param>
        /// <param name="lon2"></param>
        /// <returns>距离单位km</returns>
        public static double GetDistance(double lat1, double lon1, double lat2, double lon2)
        {
            double radLat1 = rad(lat1);
            double radLat2 = rad(lat2);
            double a = radLat1 - radLat2;
            double b = rad(lon1) - rad(lon2);
            double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
             Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.Round(s * 10000) / 10000;
            return s;
        }

        /// <summary>
        ///  计算两个点之间的距离
        /// </summary>
        /// <param name="lat1"></param>
        /// <param name="lon1"></param>
        /// <param name="lat2"></param>
        /// <param name="lon2"></param>
        /// <returns>距离单位km</returns>
        public static double GetDistance(string lat1, string lon1, string lat2, string lon2, out string desc)
        {
            desc = "";
            if (string.IsNullOrEmpty(lat1) || string.IsNullOrEmpty(lat2)) return 99999;
            double distance = GetDistance(Convert.ToDouble(lat1), Convert.ToDouble(lon1), Convert.ToDouble(lat2), Convert.ToDouble(lon2));
            if (distance > 1)
               desc= string.Format("{0}km", distance.ToString("f1"));
            else
                desc= string.Format("{0}m", (distance * 1000).ToString("f0"));
            return distance;
        }

        public static string GetDistance(decimal distance)
        {
            if (distance == 999999) return "";
            if (distance > 1)
                return string.Format("{0}km", distance.ToString("f1"));
            else
                return string.Format("{0}m", (distance * 1000).ToString("f0"));
        }


        #region 火星坐标系 (GCJ-02)与百度坐标系 (BD-09)
        const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        /// <summary>
        /// 将GCJ-02坐标转换成BD-09坐标
        /// </summary>
        /// <param name="gcjPoint">GCJ-02坐标</param>
        /// <returns>BD-09坐标</returns>
        public static LatLngPoint GCJ02ToBD09(LatLngPoint gcjPoint)
        {
            LatLngPoint _bdPoint = new LatLngPoint();
            double _x = gcjPoint.LonX, y = gcjPoint.LatY;
            double _z = Math.Sqrt(_x * _x + y * y) + 0.00002 * Math.Sin(y * x_pi);
            double _theta = Math.Atan2(y, _x) + 0.000003 * Math.Cos(_x * x_pi);
            _bdPoint.LonX = _z * Math.Cos(_theta) + 0.0065;
            _bdPoint.LatY = _z * Math.Sin(_theta) + 0.006;
            return _bdPoint;
        }

        /// <summary>
        /// 将BD-09坐标转换成GCJ-02坐标
        /// </summary>
        /// <param name="bdPoint">BD-09坐标</param>
        /// <returns>GCJ-02坐标</returns>
        public static LatLngPoint BD09ToGCJ02(LatLngPoint bdPoint)
        {
            LatLngPoint _gcjPoint = new LatLngPoint();
            double _x = bdPoint.LonX - 0.0065, _y = bdPoint.LatY - 0.006;
            double _z = Math.Sqrt(_x * _x + _y * _y) - 0.00002 * Math.Sin(_y * x_pi);
            double _theta = Math.Atan2(_y, _x) - 0.000003 * Math.Cos(_x * x_pi);
            _gcjPoint.LonX = _z * Math.Cos(_theta);
            _gcjPoint.LatY = _z * Math.Sin(_theta);
            return _gcjPoint;
        }


        public static string GetCityNameByLayLngPoint(LatLngPoint point)
        { 
           string url=string.Format( "http://api.map.baidu.com/geocoder?output=json&location={0},{1}&key=9a76a6f405d191d7a5382cf9c657b57e",point.LatY,point.LonX);

            string message = "";// HttpClientHelper.GetResponse(url);

           dynamic o = Newtonsoft.Json.JsonConvert.DeserializeObject(message);
           if (o.status == "OK")
           {
               return o.result.addressComponent.city;
           }
           return "";
        }
        
        #endregion
    }

    public class LatLngPoint
    {
        /// <summary>
        ///  经度
        /// </summary>
        public double LonX
        {
            get;
            set;
        }

        /// <summary>
        ///  维度
        /// </summary>
        public double LatY
        {
            get;
            set;
        }
    }
}
